import useCardsStore from '@renderer/store/modules/useCardsStore'
import useDiyStore from '@renderer/store/modules/useDiyStore'
import { nameCard, nameDiyCard } from '@renderer/config'
import * as PIXI from 'pixi.js'
const cardsStore = useCardsStore()
const diyStore = useDiyStore()
/** 字体资源 */
const fontsAssets = new Map<string, string>([
  ['RZFont', '/font/RZFont.ttf'],
  ['XQFont', '/font/XQFont.ttf'],
])
/** 初始化卡册所需资源 */
const initAssets = new Map<string, string>([
  ['HallBG_Back', 'HallBG_Back.png'], // 背景
  ['cardBook', 'cardBook.png'], // 卡册精灵
  ['bxlogo', 'bxlogo.png'], // logo
  ['yellow', 'yellow.png'], // 创作图标按钮
])

/** 图标资源 */
const iconsAssets = new Map<string, string>([
  ['icons', 'icon/icons.png'], // 背景
])

/** 按钮资源 */
const moduleAssets = new Map<string, string>([
  ['btns', 'sprite/btns.png'], // 按钮
  ['close', 'close.png'], // 关闭按钮
  ['diyBg', 'diyBg.png'], // diy界面背景
])

/** 卡牌资源 */
const cardModuleAssets = new Map<string, string>([
  ['category', 'sprite/category.png'], // 类别
  ['element', 'sprite/element.png'], // 五行
  ['series', 'sprite/series.png'], // 系列
  ['type', 'sprite/type.png'], // 类型
  ['skillConsume', 'sprite/skillConsume.png'], // 技能消耗
  ['sprite', 'sprite/sprite.png'], // 其他
])

const spriteJson = async <T>(fn: string): Promise<T> => {
  return await window.api.jsonAsset(fn)
}

const diyPortfolio = async (): Promise<Map<string, Wx.Card.Data[]>> => {
  return await window.api.diyPortfolio()
}

/** 载入卡牌 */
const cardsAssets = async (): Promise<void> => {
  const data = await spriteJson<Wx.Card.Data[]>('/json/cards.json')
  cardsStore.setCards(data)
  data.forEach(async (card) => {
    PIXI.Assets.add({
      alias: 'card_' + card.id,
      src: await window.api.getAsset(nameCard(card)),
    })
  })
}

/** 载入DIY卡牌 */
const diyCardsAssets = async (): Promise<void> => {
  const portfolio = await diyPortfolio()
  const data = [] as Wx.Card.Data[]
  portfolio.forEach((cardData, author) => {
    data.push(...cardData)
    diyStore.portfolio.set(author, cardData)
  })
  const diysMap = await data.reduce(async (mapPro, card) => {
    PIXI.Assets.add({
      alias: 'card_' + card.id,
      src: await nameDiyCard(card),
    })
    const map = await mapPro
    map.set(card.id, card)
    return map
  }, Promise.resolve(new Map<number, Wx.Card.Data>()))
  cardsStore.setDiys(data)
  cardsStore.setDiysData(diysMap)
}

export default {
  getFonts: async (): Promise<Record<string, ArrayBuffer>> => {
    return await window.api.getFonts(fontsAssets)
  },
  initAssets: async (): Promise<Record<string, string>> => {
    return await window.api.getAssets(initAssets)
  },
  iconAssets: async (): Promise<Record<string, string>> => {
    return await window.api.getAssets(iconsAssets)
  },
  moduleAssets: async (): Promise<Record<string, string>> => {
    return await window.api.getAssets(moduleAssets)
  },
  cardModuleAssets: async (): Promise<Record<string, string>> => {
    return await window.api.getAssets(cardModuleAssets)
  },
  spriteJson,
  cardsAssets,
  diyCardsAssets,
}
